<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        h1 {
            text-align: center;
            color: #1877f2;
        }

        form {
            display: flex;
            flex-direction: column;
        }

        input {
            margin-bottom: 1rem;
            padding: 0.5rem;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        button {
            background-color: #1877f2;
            color: white;
            border: none;
            padding: 0.5rem;
            border-radius: 4px;
            cursor: pointer;
            font-size: 1rem;
        }

        button:hover {
            background-color: #166fe5;
        }

        #message {
            margin-top: 1rem;
            text-align: center;
            color: #721c24;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            border-radius: 4px;
            padding: 0.5rem;
            display: none;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h1>欢迎登录</h1>
    <form id="loginForm">
        <input type="text" id="username" name="username" placeholder="用户名" required>
        <input type="password" id="password" name="password" placeholder="密码" required>
        <button type="submit">登录</button>
    </form>
    <div id="message"></div>
</div>

<script>
    $(document).ready(function () {
        $("#loginForm").on("submit", function (e) {
            e.preventDefault();

            var formData = {
                username: $("#username").val(),
                password: $("#password").val()
            };

            $.ajax({
                type: "POST",
                url: "login",
                data: formData,
                success: function (response) {
                    if (response === "") {
                        // 登录成功
                        window.location.href = "/index.html"; // 假设成功后跳转到首页
                        //alert("登录成功!");
                    }

                },
                error: function (xhr) {
                    switch (xhr.status) {
                        case 400:
                            alert("登录失败:请求不正确,请输入检查");
                            break;
                        case 401:
                            alert("登录失败:用户名或密码错误");
                            break;
                        default:
                            alert("登录失败: " + xhr.statusText);
                    }
                }
            });
        });
    });
</script>
</body>
</html>